/**
 * version 1.4
 */
import http from "./http.js"
let responses = null;
const REQUST_ERROR = 1001;
const throttling=1500; 
let lasttime=0;

function updataResponses(datas,data){
	lasttime=new Date().getTime();
	if(data==null){
		responses.set(datas.url, datas);
		return;  
	}
	if(data.code!=0){
		responses.set(datas.url, datas);
		return;
	}
	responses.set(datas.url, datas);
	
}
/**
 * 判断是否存在该网络请求体与请求对象
 * 请求节流
 * @param {Object} requestUrl
 * @param {Object} params
 */
function contrast(requestUrl, params) {
	//初始化缓存
	if (responses == null) {
		responses = new Map();
	}
	let result = {
		data: null,
		url: requestUrl,
		params: params
	}
	//判断是否存在
	if (!responses.has(requestUrl)) {
		return result;
	}
	
	let datas = responses.get(requestUrl);
	let val1 = JSON.stringify(datas.params);
	let val2 = JSON.stringify(params);
	//判断两次请求是否一致，如果一致就返回上一次请求数据
	if (datas.url == requestUrl && val1 == val2) {
		let nexttime=new Date().getTime();
		//判断前后两次请求时间差小于1s
		if(nexttime-lasttime<throttling){
			return datas;
		}else{
			//当次请求时间差大于1s,更新记录时间
			lasttime=nexttime;
			return result;
		}
		
	} else {
		return result;
	}

}
const postflyio = (url, params, succeed, errored) => {
	let datas = contrast(url, params);
	//判断是否包含响应体内容data，如果包含响应体，就返回已存在响应体
	if (datas.data != null) {
		succeed(datas.data);
	} else {
		http.fly.post(datas.url, params).then(res => {
			let data = res.data;
			updataResponses(datas,data);
			if (data.code != 0) {
				errored(data.code, data.msg);
			} else {
				succeed(data);
			}
		}).catch(error => {
			updataResponses(datas,null);
			errored(REQUST_ERROR, error);
		});
	}
}

const getflyio = (url, params, succeed, errored) => {
	let datas = contrast(url, params);
	if (datas.data != null) {
		succeed(datas.data);
	} else {
		http.fly.get(datas.url).then(res => {
			let data = res.data;
			updataResponses(datas,data);
			if (data.code != 0) {
				errored(data.code, data.msg);
			} else {
				succeed(data);
			}
		}).catch(error => {
			updataResponses(datas,null);
			errored(REQUST_ERROR, error);
		});


	}
}

const get = (url, params, headers, succeed, errored) => {
	let datas = contrast(url, params);
	if (datas.data != null) {
		succeed(datas.data);
	} else {
		uni.request({
			url: datas.url,
			method: "GET",
			data: params,
			header: headers,
			dataType: 'json',
			success: function(res) {
				let data = res.data;
				updataResponses(datas,data);
				if (data.code != 0) {
					errored(data.code, data.msg);
				} else {
					succeed(data);
				}
			},
			fail: function(err) {
				updataResponses(datas,null);
				errored(REQUST_ERROR, error);
			}
		});
	}
}
const post = (url, params, headers, succeed, errored) => {
	let datas = contrast(url, params);
	if (datas.data != null) {
		succeed(datas.data);
	} else {
		uni.request({
			url: datas.url,
			data: params,
			method: "POST",
			header: headers,
			dataType: 'json',
			success: function(res) {
				let data = res.data;
				updataResponses(datas,data);
				if (data.code != 0) {
					errored(data.code, data.msg);
				} else {
					succeed(data);
				}
			},
			fail: function(err) {
				updataResponses(datas,null);
				errored(REQUST_ERROR, error);
			}
		})
	}
}

module.exports = {
	post,
	get,
	getflyio,
	postflyio
}
